﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Collections.ObjectModel;
using WcfServiceAfvanding.Code;

using System.Threading;

namespace WcfServiceAfvanding
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IAfvanding
    {
        ObservableCollection<Pumpestation> pumpestationer;
        ObservableCollection<Pumpe> pumper;
        Pumpe pumpe;
        Kanal aktuelKanal;
        Pumpestation pumpestation;

        public Service1()
        {
            pumpe = new Pumpe();
            pumpestation = new Pumpestation();
            pumpestationer = new ObservableCollection<Pumpestation>();
            pumper = new ObservableCollection<Pumpe>();
            aktuelKanal = new Kanal();
        }
        public void OpretPumpestation(string netværksadresse, string ipAdresse, int portnummer, string vandstand)
        {
            pumpestation = new Pumpestation();
            pumpestation.Netværksadresse = netværksadresse;
            pumpestation.Ipadresse = ipAdresse;
            pumpestation.Portnummer = portnummer;
            pumpestation.Vandstand = vandstand;
            pumpestationer.Add(pumpestation);
            pumpestation.OpretPumpstation();
        }

        public void OpretPumpe(double vandkapacitet, bool kører, int pumpestationsId)
        {
            pumpe = new Pumpe();
            pumpe.Vandkapcitet = vandkapacitet;
            pumpe.Kører = kører;
            pumper.Add(pumpe);
            pumpe.OpretPumpe(vandkapacitet, kører, pumpestationsId);
        }

        public void RetPumpestation(string netværksadresse, string ipAdresse, int portnummer, string vandstand)
        {

        }

        public void RetPumpe(double vandkapacitet, bool kører)
        {

        }

        public void SletPumpestation(int pumpestationsId)
        {
            pumpestation.SletPumpestation(pumpestationsId);
        }

        public void SletPumpe(int pumpeId)
        {
            pumpe.SletPumpe(pumpeId);
        }

        public ObservableCollection<Pumpestation> MinePumpestationer()
        {
            return pumpestationer = Pumpestation.MinePumpestationer();
        }

        public ObservableCollection<Pumpe> MinePumper(int pumpestationsId)
        {
            return pumper = Pumpe.HentPumper(pumpestationsId);
        }

        public void StartPumpe(int pumpeId)
        {
            pumpe = FindPumpe(pumpeId);
            pumpe.Kører = true;
            Thread traad = new Thread(KørTraad);
            traad.Start();
        }

        public Pumpe FindPumpe(int pumpeId)
        {
            bool fundet = false;
            int i = 0;

            while (!fundet)
            {
                if (pumper[i].PumpeId == pumpeId)
                    fundet = true;
                else
                    i++;
            }

            return pumper[i];
        }

        public void KørTraad()
        {
            while (pumpe.Kører == true)
            {
                if (aktuelKanal.Vandstand != 0)
                {
                    Thread.Sleep(10000);
                    aktuelKanal.Vandstand -= pumpe.Vandkapcitet;
                }
            }
        }

        public void StopPumpe(int pumpeId)
        {
            pumpe.Kører = false;
        }
    }
}
